package com.qilu.pe;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.qilu.pe.dao.bean.EventModel;
import com.qilu.pe.support.widget.BottomNavigationViewEx;
import com.qilu.pe.ui.fragment.Home1Fragment;
import com.qilu.pe.ui.fragment.Home2Fragment;
import com.qilu.pe.ui.fragment.Home3Fragment;
import com.qilu.pe.ui.fragment.Home4Fragment;
import com.qilu.pe.ui.fragment.Home5Fragment;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.support.view.NoScrollViewPager;
import com.ruitu.arad.util.SizeUtils;
import com.ruitu.arad.util.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends BaseActivity {

    private BottomNavigationViewEx bnve;
    private NoScrollViewPager vp;

    private VpAdapter adapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bnve = findViewById(R.id.bnve);
        vp = findViewById(R.id.vp);

        vp.setOffscreenPageLimit(9);
        vp.setScanScroll(false);

        bnve.enableAnimation(false);
        bnve.enableShiftingMode(false);
        bnve.enableItemShiftingMode(false);

        bnve.setIconsMarginTop(SizeUtils.dp2px(10));
        bnve.setIconSize(23, 23);
        bnve.setTextSize(10);
        bnve.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                return true;
            }
        });

        adapter = new VpAdapter(getSupportFragmentManager());
        vp.setAdapter(adapter);
        bnve.setupWithViewPager(vp);

        vp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {

            }
        });

        Arad.bus.register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMsg(EventModel ev) {
        if (ev.getEventCode() == 20101651) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (Arad.bus.isRegistered(this)) {
            Arad.bus.unregister(this);
        }
    }

    private class VpAdapter extends FragmentPagerAdapter {

        public VpAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {// 首页
                return Home1Fragment.newInstance();
            } else if (position == 1) {// 患者
                return Home2Fragment.newInstance();
            } else if (position == 2) {// AI体检
                return Home3Fragment.newInstance();
            } else if (position == 3) {// 商城
                return Home4Fragment.newInstance();
            } else if (position == 4) {// 我的
                return Home5Fragment.newInstance();
            }

            return Home2Fragment.newInstance();
        }

        @Override
        public int getCount() {
            return 5;
        }
    }

    private long waitTime = 2000;
    private long touchTime = 0;

    @Override
    public void onBackPressed() {
        if (null != vp && vp.getCurrentItem() == 0) {
            long currentTime = System.currentTimeMillis();
            if ((currentTime - touchTime) >= waitTime) {
                ToastUtils.showShortSafe("再按一次退出程序");
                touchTime = currentTime;
            } else {
//                Intent backHome = new Intent(Intent.ACTION_MAIN);//启动一个意图,回到桌面
//                backHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                backHome.addCategory(Intent.CATEGORY_HOME);
//                startActivity(backHome);
                super.onBackPressed();
            }
        } else {
            vp.setCurrentItem(0);
        }
    }
}